package route

import (
	"encoding/json"
	"fmt"
	"gitee.com/ljfirst/algo-go-protal/integration/pb"
	"gitee.com/ljfirst/algo-go-protal/service"
	"github.com/gin-gonic/gin"
	"io/ioutil"
)

/**
 * @author 蔚蔚樱
 * @version V1.0
 * @date 2023/5/24 02:09
 * @author-Email micromicrohard@outlook.com
 * @blogURL https://blog.csdn.net/Micro_Micro_Hard
 * @description
 * */

var (
	serviceTool *service.DispatchAlgorithm
)

func Begin() {
	initService()
	ginServer := gin.Default()
	ginServer.POST("/exec_algo", ExecAlgo)
	ginServer.Run(":8992")
}

func initService() {
	serviceTool = &service.DispatchAlgorithm{}
}

func ExecAlgo(context *gin.Context) {
	body := context.Request.Body
	bytes, err := ioutil.ReadAll(body)
	if err != nil {
		fmt.Printf("read body err, %v\n", err)
		return
	}

	req := &pb.DispatchAlgorithmRequest{}
	rsp := &pb.DispatchAlgorithmResponse{}
	err = json.Unmarshal(bytes, &req)
	if err != nil {
		return
	}
	serviceTool.Dispatch(context, req, rsp)
	context.JSON(200, rsp)
}
